/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UserListPanel.java
 *
 * Created on Apr 29, 2011, 3:34:54 PM
 */
package edu.chalmers.se.tda367.gui.panel;

import edu.chalmers.se.tda367.main.Pluggis;
import edu.chalmers.se.tda367.database.Database;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import edu.chalmers.se.tda367.observer.GlobalObserverList;
import java.awt.Color;
import java.util.List;
import javax.swing.JPanel;

import edu.chalmers.se.tda367.model.User;

/**
 *
 * @author pierrereimertz
 */
public class UserListPanel extends javax.swing.JPanel implements PropertyChangeListener {

    GlobalObserverList observers = GlobalObserverList.getInstance();
    List<User> database = Database.getInstance().getDatabase();

    /** Creates new form UserListPanel */
    public UserListPanel() {

        initComponents();

        observers.addObserver(this);
        for (int i = 0; i < database.size(); i++) {
            jUserPanel.add(database.get(i).getPanel());
        }


    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jUserPanel = new javax.swing.JPanel();

        setName("Form"); // NOI18N
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(550, 515));

        jUserPanel.setName("jUserPanel"); // NOI18N
        jUserPanel.setOpaque(false);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jUserPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 550, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jUserPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 515, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jUserPanel;
    // End of variables declaration//GEN-END:variables

    public void propertyChange(PropertyChangeEvent pce) {
        String change = pce.getPropertyName();

        if (change.equals("GUI.RepaintView")) {
            this.revalidate();
            this.repaint();
        }
        if (change.equals("Database.NewUser")) {
            User u = (User) pce.getNewValue();

            jUserPanel.add(u.getPanel());
            Pluggis.getInstance().setSelectedUser(u);
            Pluggis.getInstance().repaintViews();


        }
        if (change.equals("GUI.SelectedUserChanged")) {
            if (pce.getOldValue() != null) {
                User oldUser = (User) pce.getOldValue();
                if (oldUser.getStatus()) {
                    oldUser.getPanel().setBackground(new Color(255, 255, 153));
                } else {
                    oldUser.getPanel().setBackground(new Color(224, 224, 255));
                }
            }
            if (pce.getNewValue() != null) {
                User newUser = (User) pce.getNewValue();
                if (newUser.getStatus()) {
                    newUser.getPanel().setBackground(new Color(255, 255, 0));
                } else {
                    newUser.getPanel().setBackground(new Color(124, 124, 255));
                }
                Pluggis.repaintViews();
            }


        }
    }
}
